package web;

import nl.justobjects.pushlet.pushmanager.PushManager;
import nl.justobjects.pushlet.pushmanager.PushUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import service.impl.ChatImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/im")
public class IMController {
	
	@Autowired
	private ChatImpl chatImpl;
	
	@RequestMapping("/index.do")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("pid", request.getParameter("pid"));
		// return new ModelAndView("chat", params);
        return new ModelAndView("chat_jsonformat", params);
	}
	
	@RequestMapping("/login.do")
	public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> params = PushUtils.getPushParams(request, new String[]{"name", "logout"});
		chatImpl.sendMessage(params);
	}
	
	@RequestMapping("/chat.do")
	public void chat(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, Object> params = PushUtils.getPushParams(request, new String[]{"name", "msg"});
		chatImpl.sendMessage(params);
        Map<String, Serializable> data = new HashMap<String, Serializable>();
        data.put("name", "system");
        data.put("msg", new Date().toString());
	}
	
	@RequestMapping("/ptt.do")
	public String pushThreadTest(HttpServletRequest request, HttpServletResponse response) throws InterruptedException {
        final String threadId = "ptt";
        this.chatImpl.createSourceThread(threadId);

        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(60000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                PushManager.getInstance().pauseEvent(threadId);
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(120000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                PushManager.getInstance().restartEvent(threadId);
            }
        }.start();

        new Thread() {
            @Override
            public void run() {
                try {
                    Thread.sleep(300000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                PushManager.getInstance().removeEvent(threadId);
            }
        }.start();
		/*return "pushthread";*/
        return "jsonformat";
	}
}
